package com.darin.camera;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.foreveross.scaner.R;
import com.google.zxing.Result;

public class Scanner extends Activity implements CameraActionListener, CameraManagerListener {
	private static final String TAG = "ScanerActivity";
	public static final int RESULT_CODE=1;
	private CameraManager cameraManager;
	private boolean isPad = false;
	private CameraAction cameraActionThread;
	private ViewfinderView viewfinderView;
	private ImageView imageView;
	private TextView resultTxt;
	private View mReScanBtn;
	private View mScanOkBtn;
	private Point screenResolution;
	private RelativeLayout surfaceContent;
	private boolean debug=false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.i(TAG, "onCreate>>>");
		super.onCreate(savedInstanceState);
		isPad = CommonUtil.isPad(this);
		// isPad = true;
		// getWindow().setFormat(PixelFormat.TRANSLUCENT);
		
		if (isPad) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		} else {
//			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		}
		Window window = getWindow();
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

		// getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
	
		screenResolution=CommonUtil.getScreenResolution(getWindowManager());
		Log.v(TAG, "screen resolution>>"+screenResolution);
		setContentView(R.layout.foreveross_scaner);
		surfaceContent=(RelativeLayout) findViewById(R.id.camera_surface_content);
		viewfinderView = (ViewfinderView) findViewById(R.id.camera_viewfinder);
		imageView = (ImageView) findViewById(R.id.foreveross_scaner_mDebugImage);
		resultTxt = (TextView) findViewById(R.id.foreveross_scaner_mScanText);
		mReScanBtn=findViewById(R.id.foreveross_scaner_mReScan);
		mScanOkBtn=findViewById(R.id.foreveross_scaner_mScanOk);
		
		cameraManager = new CameraManager(this, surfaceContent,getWindowManager());
		cameraManager.setCameraManagerListener(this);
		imageView.setVisibility(View.GONE);
		
		
		mReScanBtn.setOnClickListener(onClick);
		mScanOkBtn.setOnClickListener(onClick);
	}

	private View.OnClickListener onClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if(v.getId()==R.id.foreveross_scaner_mScanOk){
				Intent intent=new Intent();
				intent.putExtra("result", resultTxt.getText().toString());
				setResult(RESULT_CODE, intent);
				finish();
			}else{
				resultTxt.setText("");
				cameraActionThread.setupCameraPreViewCallBack();
			}
		}
	};

	@Override
	public void initCameraActionSuccess(Handler handler) {
		Log.d(TAG, "initSuccess>>");
		cameraManager.startPreivew();
		cameraActionThread.setupCameraCallBack();

	}

	@Override
	public void onCameraActionQuit(Handler handler) {

	}

	@Override
	public void initCameraSuccess() {
		if (cameraActionThread != null) {
			cameraActionThread.quitCameraAction();
		}
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		cameraActionThread = new CameraAction(this, cameraManager);
		cameraActionThread.setViewFinderView(viewfinderView);
		viewfinderView.setFrame(cameraManager.getFrameRect());
		cameraActionThread.start();

	}

	@Override
	public void beforeReleseCamera() {
		if (cameraActionThread != null) {
			cameraActionThread.clearAllMessage();
		}

	}

	@Override
	public void onDecodeSuccess(final Result result) {
		Log.i(TAG, "decode success>>" + result.getText());

		resultTxt.post(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				resultTxt.setText(result.getText());
			}
		});

	}

	@Override
	protected void onResume() {
		Log.i(TAG,"onResume>>>");
		
		
		if (cameraManager.isInitCamera() && !cameraManager.isStartPrevious()) {
			cameraManager.startPreivew();
			cameraActionThread.setupCameraCallBack();
		}else{
			cameraManager.init();
		}
		super.onResume();
	}

	@Override
	protected void onStop() {
		Log.i(TAG,"onStop>>>");
		if (cameraManager.isInitCamera() && cameraManager.isStartPrevious()) {
			cameraActionThread.clearAllMessage();
			cameraManager.stopPreView();

		}
		super.onStop();
	}

	@Override
	public void onCameraPreviewBitmap(final Bitmap bitmap) {
		if (bitmap != null&&debug) {
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					imageView.setVisibility(View.VISIBLE);
					imageView.setImageBitmap(bitmap);

				}
			});

		}

	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		Log.i(TAG, "onConfigurationChanged>>");
		
		super.onConfigurationChanged(newConfig);
		cameraManager.init();
	}
	
	

}
